import model.Book;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.util.List;

@WebServlet("/cart")
public class CartServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 设置响应编码为UTF-8，避免中文乱码
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        out.println("<html><head><title>购物车</title><style>" +
                "body { font-family: Arial, sans-serif; }" +
                "table { border-collapse: collapse; width: 100%; }" +
                "th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }" +
                "th { background-color:  #f2f2f2; }" +
                "</style></head><body>");

        out.println("<h2>购物车</h2>");
        out.println("<table><tr><th>ID</th><th>书名</th><th>价格</th></tr>");

        List<Book> cartBooks = PurcharseServlet.getCart();
        for (Book book : cartBooks) {
            out.println("<tr><td>" + book.getId() + "</td><td>" + book.getName() + "</td><td>" + book.getPrice() + "</td></tr>");
        }

        out.println("</table>");

        out.println("<a href='/listBooks'>继续购物</a>");

        out.println("</body></html>");
    }
}